package org.aceor.mddal.gateway.resultset;

import org.aceor.mddal.common.sqlparser.result.context.MergeContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by lxue on 16/4/16.
 */
public final class IteratorResultSet extends BaseResultSet implements ResultSet {

    public IteratorResultSet(final List<ResultSet> resultSets, final MergeContext mergeContext) {
        super(resultSets, mergeContext.getLimit());
    }

    @Override
    protected boolean nextRowSet() throws SQLException {
        if (getCurrentResultSet().next()) {
            return true;
        }
        for (int i = getResultSets().indexOf(getCurrentResultSet()) + 1; i < getResultSets().size(); i++) {
            ResultSet each = getResultSets().get(i);
            if (each.next()) {
                setCurrentResultSet(each);
                return true;
            }
        }
        return false;
    }
}
